﻿// Decompiled with JetBrains decompiler
// Type: MCM.Common.PropertyRef
// Assembly: MCMv5, Version=5.10.2.0, Culture=neutral, PublicKeyToken=null
// MVID: 7AB326B9-B2B4-41C2-9177-68E944071101
// Assembly location: D:\steam\steamapps\workshop\content\261550\2859238197\bin\Win64_Shipping_Client\MCMv5.dll

using System;
using System.ComponentModel;
using System.Reflection;
using System.Runtime.CompilerServices;

#nullable enable
namespace MCM.Common
{
  public class PropertyRef : IRef, INotifyPropertyChanged, IEquatable<PropertyRef>
  {
    public event PropertyChangedEventHandler? PropertyChanged;

    public PropertyInfo PropertyInfo { get; }

    public object Instance { get; }

    public Type Type => this.PropertyInfo.PropertyType;

    public object? Value
    {
      get => this.PropertyInfo.GetValue(this.Instance);
      set
      {
        if (!this.PropertyInfo.CanWrite)
          return;
        this.PropertyInfo.SetValue(this.Instance, value);
        this.OnPropertyChanged(nameof (Value));
      }
    }

    public PropertyRef(PropertyInfo propInfo, object instance)
    {
      this.PropertyInfo = propInfo;
      this.Instance = instance;
    }

    protected virtual void OnPropertyChanged([CallerMemberName] string? propertyName = null)
    {
      PropertyChangedEventHandler propertyChanged = this.PropertyChanged;
      if (propertyChanged == null)
        return;
      propertyChanged((object) this, new PropertyChangedEventArgs(propertyName));
    }

    public bool Equals(PropertyRef? other)
    {
      if ((object) other == null)
        return false;
      return (object) this == (object) other || this.PropertyInfo.Equals((object) other.PropertyInfo) && this.Instance.Equals(other.Instance);
    }

    public override bool Equals(object? obj)
    {
      if (obj == null)
        return false;
      if ((object) this == obj)
        return true;
      return !(obj.GetType() != this.GetType()) && this.Equals((PropertyRef) obj);
    }

    public override int GetHashCode()
    {
      return (269 * 47 + this.PropertyInfo.GetHashCode()) * 47 + this.Instance.GetHashCode();
    }

    public static bool operator ==(PropertyRef? left, PropertyRef? right)
    {
      return object.Equals((object) left, (object) right);
    }

    public static bool operator !=(PropertyRef? left, PropertyRef? right)
    {
      return !object.Equals((object) left, (object) right);
    }
  }
}
